package garin.parse.sql;

import garin.parse.CommentToken;
import garin.parse.Token;
import garin.parse.io.SetValueObserver;
import garin.parse.io.Source;
import garin.parse.sql.sql2003.SqlStatement;
import garin.parse.sql.sql2003.StatementEndToken;

import java.io.IOException;

public class SqlStatementReaderWithCommentFilter extends SqlStatementReader
		implements SetValueObserver {
	private final StringBuilder buffer = new StringBuilder();
	
	public SqlStatementReaderWithCommentFilter(final Source s,
			final Class<? extends SqlStatement> clz,
			final SqlStatementReaderClient client) throws Exception {
		super(s, clz, client);
	}
	
	@Override
	public void onSetValue(final Token token) throws IOException {
		if (token instanceof StatementEndToken) {
			client.onEndStatement(buffer.toString() + ";");
			buffer.setLength(0);
		} else if (token instanceof CommentToken) {
			// Ignore comments
		} else {
			final String contents = token.contents();
			buffer.append(contents);
		}
	}
}
